<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template v-slot:condition="{row}">
      <span v-html="condition_info(JSON.parse(row.condition))"></span>
    </template>
    <template #action="{row}">
      <el-button v-if="in_right_admin('CodeGroup/make_code')" @click="make_code(row)" size="small">生成</el-button>
      <el-button v-if="in_right_admin('CodeGroup/list')" @click="to_code_list(row)" size="small">兑换码列表</el-button>
      <el-button v-if="in_right_admin('CodeGroup/export_code')" @click="export_code(row)" size="small">兑换码导出</el-button>
    </template>
  </list_url>
</template>
<script>
import {_config, _href} from "@/js/s94_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('CodeGroup/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('CodeGroup/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('CodeGroup/add') && _config('this_tenant.id')) btns['添加'] = function(){ iframe('code_group/edit') }
      if(in_right_admin('CodeGroup/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('CodeGroup/recycle')) btns['回收站'] = function(){ iframe('code_group/index?is_del=1') }
    }
    var status_config = _config('status_alias_map.code_group.status');
    let forms_config = {};
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      title: {name:'title', label:'标题', width:'120px'},
      total_num: {name:'total_num', label:'总数', width:'80px'},
      use_num: {name:'use_num', label:'已使用', width:'80px'},
      code_template: {name:'code_template', label:'兑换码模板', type:'text_more'},
      execute_title: {name:'execute_title', label:'兑换内容', type:'text_more'},
      status: {name:'status', label:'状态',config:status_config,type:'radio_switch',can_edit:true,width:'80px',onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'180px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/code_group/'+(is_del?'recycle':'list'),
        title: "兑换码组"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: (list)=>{
          list.forEach(row=>{
            row.code_template = this.code_template(row);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    code_template(row){
      let res = row.code_prefix+'+';
      res += '长度为'+row.code_unique_length+'的';
      if (~row.code_unique_func.indexOf('_confuse')){
        res += '混淆的';
      }
      if (~row.code_unique_func.indexOf('index')){
        res += '当前组的序列号'
      }else if (~row.code_unique_func.indexOf('id')){
        res += '所有兑换码的序列号'
      }
      if (~row.code_unique_func.indexOf('_zip')){
        res += '的36进制'
      }
      return res;
    },
    to_code_list(row){
      iframe('code/index?code_group_id='+row.id)
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('CodeGroup/edit')) return;
      ajax(`/admin/code_group/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (col.type=='radio_switch' && res.data==0) app.$refs['list_url'].list();
        if (col.type!='radio_switch' && res.data>0) app.$refs['list_url'].list();
      })
    },
    make_code(row){
      let app = this;
      this.$prompt('生成数量', '生成兑换码', {}).then((res)=>{
        if (!res.value && res.value <= 0) return ;
        ajax({
          url: '/admin/code_group/make_code',
          data: {id: row.id, num: res.value},
          loading: true,
        }, function(res){
          app.$refs['list_url'].list();
        })
      }).catch(()=>{})
    },
    export_code(row){
      let app = this;
      window.open(_href("/admin/code_group/export_code?id="+row.id), '_blank')
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    if (!_config('this_tenant.id')){
      app.config.list_cols.tenant_id = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
  }
}
</script>
